package cn.bdqn.demo.seckill;


import cn.bdqn.demo.seckill.SecKillService;
import cn.bdqn.demo.service.OrderService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class SecKillController {
    @Resource
    StockBean stockBean;

    @Resource
    SecKillService secKillService;
    @Resource
    OrderService orderService;
    /**
     * 加锁抢购
     * @param
     * @return
     */
    @GetMapping("/secKill")
    public Object secKill() {
        int ret = secKillService.secKill(stockBean.getProductId(), UniqueKey.genUniqueKey());
        return ret;
    }

    /**
     * 不加锁抢购
     * @param
     * @return
     */
    @GetMapping("/secKill2")
    public Object secKill2() {
        int ret = secKillService.secKill(stockBean.getProductId(), UniqueKey.genUniqueKey(),false);
        return ret;
    }

    @GetMapping("/result")
    public Object result() throws Exception {
        int count = orderService.getCount();
        int stock = secKillService.getStock(stockBean.getProductId());
        String result = "商品"+stockBean.getProductId()+"共"+stockBean.getInitStock()+"个,目前剩余" + stock;
        result += ",成功下单" + count + "个";
        return result;
    }
}
